package checkcode;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebFilter(filterName = "characterFilter", initParams = { @WebInitParam(name = "encode", value = "UTF-8") }, urlPatterns = "/*")
public class CharacterSetFilter implements Filter {

	private String encode;

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */

	public void init(FilterConfig filterConfig) throws ServletException {

		// TODO Auto-generated method stub

		encode = filterConfig.getInitParameter("encode");

		System.out.println("encode " + encode);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
	 * javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */

	public void doFilter(ServletRequest request, ServletResponse response,

	FilterChain chain) throws IOException, ServletException {

		// TODO Auto-generated method stub

		HttpServletRequest req = (HttpServletRequest) request;

		HttpServletResponse resp = (HttpServletResponse) response;

		req.setCharacterEncoding(encode);

		resp.setCharacterEncoding(encode);

		chain.doFilter(req, resp);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#destroy()
	 */

	public void destroy() {

		// TODO Auto-generated method stub

		System.out.println("destroy");

	}

}
